package com.situ.staffmgr.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.staffmgr.entity.Admin;

/*@WebFilter("/*")*/
public class AdminFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req=(HttpServletRequest)request;
		HttpServletResponse resp=(HttpServletResponse)response;
		//过滤的实现过程
		HttpSession session=req.getSession();
		//获取当前登录用户
		Admin admin=(Admin) session.getAttribute("admin");
		//需要放行两个路径login.jsp,admin.do
		String url=req.getRequestURL().toString();
		if (url.endsWith("login.jsp")||url.endsWith("admin.do")) {
			chain.doFilter(request, response);
			return;
		}
		
		if (admin==null) {
			//没有登陆跳转到登录页面
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
		}else {
			//已经登陆
			//已经登陆 放行
			chain.doFilter(request,response);
			//看看其他还有filter需要执行，如果有执行
			//没有则进入页面
		}
	}
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}
